package com.cloudptt.wp.ui.address.map;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.MapView;
import org.osmdroid.views.overlay.Polyline;

import com.cloudptt.api.product.Product;
import com.cloudptt.api.product.ProductConst;
import com.cloudptt.api.product.operator.AddrOperator;
import com.cloudptt.wp.R;
import com.cloudptt.wp.SysApplication;
import com.cloudptt.wp.ui.address.adapter.TrackContactAdapter;
import com.cloudptt.wp.ui.address.map.data.Point;
import com.cloudptt.wp.ui.address.map.data.PointInfo;
import com.mcptt.db.model.AddrInfo;
import com.mcptt.db.model.UserInfo;

import android.content.Context;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class TrackUtil {

	private static final int[] COLORS = new int[]{
			R.color.color_black,
			R.color.color_blue,
			R.color.color_cyan,
			R.color.color_green,
			R.color.color_magenta,
			R.color.color_orange,
			R.color.color_purple,
			R.color.color_red,
			R.color.color_brilliant,
			R.color.color_yellow
	};

	private MapView mapView;
	private Context context;
	private ListView mListView;

	public Map<String, Polyline> mTrackMap = new HashMap<String, Polyline>();
	
	private TrackContactAdapter adapter;

	private SysApplication app;

	private List<UserInfo> list = new ArrayList<UserInfo>();

	public TrackUtil(Context ctx) {
		// TODO Auto-generated constructor stub
		context = ctx;
	}

	public void setMapView(MapView mv){
		mapView = mv;
	}

	public void initTrackLv(ListView listview) {
		app = (SysApplication)context.getApplicationContext();
		if(app != null){
			Product product = app.getProduct();
			if (product != null) {
				AddrOperator operator = (AddrOperator) product.getOperator(ProductConst.Func_Addr);
				// list = operator.getAddressList(UserInfo.UserType_User);
				list = operator.getUserInfoList();
			}
			adapter = new TrackContactAdapter(context, list);
			mListView = listview;
			mListView.setAdapter(adapter);
			mListView.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
					TextView tvName = (TextView) view.findViewById(R.id.tv_user_name);
					String name = tvName.getText().toString().trim();
					if (tvName.isSelected()) {
						tvName.setSelected(false);
						adapter.setState(position, false);
						deleteTrack(name);
					} else {
						tvName.setSelected(true);
						adapter.setState(position, true);
						showTrack(name , position);
					}
				}
			});
		}
	}
	
	private List<AddrInfo> getUserList(List<AddrInfo> list){
		List<AddrInfo> userList = new ArrayList<AddrInfo>();
		for(int i = 0 ; i < list.size(); i++){
			AddrInfo userInfo = list.get(i);
//			if(userInfo.isDac() || userInfo.isCamera()){
//				continue;
//			}
			if(UserInfo.UserType_User == userInfo.getType())
			{
				userList.add(list.get(i));
			}
		}
		
		return userList;
	}

	public void showTrack(String name , int position) {
		List<PointInfo> testPointInfoList = PointInfo.getTestPointInfoList(name);
		List<GeoPoint> geoPoints = new ArrayList<GeoPoint>();
		for (int i = 0; i < testPointInfoList.size(); i++) {
			Point point = testPointInfoList.get(i).getPoint();
			GeoPoint geoPoint = new GeoPoint(point.getLat(), point.getLng());
			geoPoints.add(geoPoint);
		}
		try {
			Polyline line = new Polyline();
			line.setPoints(geoPoints);
			line.setColor(context.getResources().getColor(COLORS[position%COLORS.length]));
			mapView.getOverlayManager().add(line);
			mTrackMap.put(name, line);
			mapView.invalidate();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void deleteTrack(String name) {
		Polyline polyline = mTrackMap.get(name);
		if (polyline != null && mapView != null) {
			mapView.getOverlayManager().remove(polyline);
			mapView.invalidate();
			mTrackMap.remove(name);
		}
	}

	public void deleteAllTrack() {
		if(mListView != null){
			mListView.setVisibility(View.GONE);
		}
		if (mTrackMap.isEmpty()) {
			return ;
		}
		Set<String> keySet = mTrackMap.keySet();
		Iterator<String> iterator = keySet.iterator();
		while (iterator.hasNext()) {
			String next = iterator.next();
			Polyline polyline = mTrackMap.get(next);
			if (polyline != null && mapView != null) {
				mapView.getOverlayManager().remove(polyline);
			}
		}
		mapView.invalidate();
		mTrackMap.clear();
		adapter.resetAdapter();
	}

	public void showListView(boolean isShow){
		if(isShow){
			if(mListView != null){
				mListView.setVisibility(View.VISIBLE);
			}
		}else{
			deleteAllTrack();
		}
	}
	
	public void updateData(){
		list = ((AddrOperator) app.getProduct().getOperator(ProductConst.Func_Addr)).getUserInfoList();
		adapter.setData(list);
	}

}
